package com.llynosy.interview.controller.api;

import com.llynosy.interview.po.Result;
import com.llynosy.interview.po.StatusCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.Semaphore;

@Controller
@RequestMapping("/api/test")
public class APITest {

    static Semaphore semaphore=new Semaphore(10);

    static int requestCount=0;

    @RequestMapping("/high")
    @ResponseBody
    public Result semaphoreTest(){
        try {
            //获得许可
            semaphore.acquire();
            System.out.println("操作ing...");
            Thread.sleep(500);
            System.out.println("count is.."+requestCount++);
            return new Result(true, StatusCode.OK,"OK...");
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            //释放许可
            semaphore.release();
        }
        return new Result(false, StatusCode.ERROR,"ERROR...");
    }

}
